package com.worldcup.model.dto.question;

import com.worldcup.model.enums.QuestionFormTypeEnum;
import com.worldcup.model.question.Question;
import com.worldcup.model.question.QuestionOption;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author duwan.zq
 * @date 2019/03/05
 */
@Data
public class QuestionDTO implements Serializable{

    private Long id;

    private String questionTitle;

    private Integer formType;

    private Integer questionType;

    private Integer questionWeight;

    private Boolean required;

    private Boolean multiple;

    /** 选项  选择题列表内为QuestionOption对象 select类型列表为String对象 **/
    private List<Object> options;

    public QuestionDTO(Question question, List<QuestionOption> options) {
        this.id = question.getId();
        this.questionTitle = question.getQuestionTitle();
        this.formType = question.getFormType();
        this.questionType = question.getQuestionType();
        this.questionWeight = question.getQuestionWeight();
        this.required = question.getRequired();
        this.multiple = question.getMultiple();

        if(question.getFormType().equals(QuestionFormTypeEnum.OPTION_QUESTION.getKey())){
            this.options = initOptionOptions(options);
        }else if(question.getFormType().equals(QuestionFormTypeEnum.CHOICE_QUESTION.getKey())){
            this.options = initChoiceOptions(options);
        }
    }

    private List<Object> initChoiceOptions(List<QuestionOption> options) {
        if(options == null || options.size() == 0){
            return null;
        }
        List<Object> optionDTOs = new ArrayList<>(options.size());
        for(QuestionOption option : options){
            optionDTOs.add(new QuestionOptionDTO(option));
        }
        return optionDTOs;
    }

    private List<Object> initOptionOptions(List<QuestionOption> options){
        if(options == null || options.size() == 0){
            return null;
        }
        return options.stream().map(QuestionOption::getOptionContent).collect(Collectors.toList());
    }


}
